package com.eshop.modules.activity.service.impl;

import org.slf4j.LoggerFactory;
import java.lang.invoke.SerializedLambda;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import java.math.BigDecimal;
import com.eshop.modules.activity.domain.StoreBargainUserHelp;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.eshop.modules.activity.domain.StoreBargain;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eshop.modules.activity.vo.StoreBargainUserQueryVo;
import java.util.List;
import java.io.Serializable;
import com.eshop.enums.OrderInfoEnum;
import com.eshop.api.EshopException;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.eshop.modules.activity.service.StoreBargainUserHelpService;
import com.eshop.modules.activity.service.StoreBargainService;
import org.springframework.beans.factory.annotation.Autowired;
import com.eshop.dozer.service.IGenerator;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;
import com.eshop.modules.activity.service.StoreBargainUserService;
import com.eshop.modules.activity.domain.StoreBargainUser;
import com.eshop.modules.activity.service.mapper.StoreBargainUserMapper;
import com.eshop.common.service.impl.BaseServiceImpl;

@Service
public class StoreBargainUserServiceImpl extends BaseServiceImpl<StoreBargainUserMapper, StoreBargainUser> implements StoreBargainUserService
{
    private static final Logger log;
    @Autowired
    private IGenerator generator;
    @Autowired
    private StoreBargainUserMapper storeBargainUserMapper;
    @Autowired
    private StoreBargainService storeBargainService;
    @Autowired
    private StoreBargainUserHelpService storeBargainUserHelpService;
    
    public void setBargainUserStatus(final Long bargainId, final Long uid) {
        final StoreBargainUser storeBargainUser = this.getBargainUserInfo((long)bargainId, uid);
        if (ObjectUtil.isNull(storeBargainUser)) {
            return;
        }
        if (storeBargainUser.getStatus() != 1) {
            return;
        }
        final double price = NumberUtil.sub((Number)NumberUtil.sub((Number)storeBargainUser.getBargainPrice(), (Number)storeBargainUser.getBargainPriceMin()), (Number)storeBargainUser.getPrice()).doubleValue();
        if (price > 0.0) {
            return;
        }
        storeBargainUser.setStatus(3);
        this.storeBargainUserMapper.updateById(storeBargainUser);
    }
    
    public void bargainCancel(final Long bargainId, final Long uid) {
        final StoreBargainUser storeBargainUser = this.getBargainUserInfo(bargainId, uid);
        if (ObjectUtil.isNull(storeBargainUser)) {
            throw new EshopException("数据不存在");
        }
        if (!OrderInfoEnum.BARGAIN_STATUS_1.getValue().equals(storeBargainUser.getStatus())) {
            throw new EshopException("状态错误");
        }
        this.storeBargainUserMapper.deleteById((Serializable)storeBargainUser.getId());
    }
    
    public List<StoreBargainUserQueryVo> bargainUserList(final Long bargainUserUid, final int page, final int limit) {
        final Page<StoreBargainUser> pageModel = (Page<StoreBargainUser>)new Page((long)page, (long)limit);
        return this.storeBargainUserMapper.getBargainUserList(bargainUserUid, pageModel);
    }
    
    public boolean isBargainUserHelp(final Long bargainId, final Long bargainUserUid, final Long uid) {
        final StoreBargainUser storeBargainUser = this.getBargainUserInfo(bargainId, bargainUserUid);
        final StoreBargain storeBargain = (StoreBargain)this.storeBargainService.getById((Serializable)bargainId);
        if (ObjectUtil.isNull(storeBargainUser) || ObjectUtil.isNull(storeBargain)) {
            return false;
        }
        final int count = (((this.storeBargainUserHelpService.lambdaQuery().eq(StoreBargainUserHelp::getBargainId, bargainId)).eq(StoreBargainUserHelp::getBargainUserId, storeBargainUser.getId())).eq(StoreBargainUserHelp::getUid, uid)).count();
        return count == 0;
    }
    
    public void setBargain(final Long bargainId, final Long uid) {
        final StoreBargainUser storeBargainUser = this.getBargainUserInfo(bargainId, uid);
        if (storeBargainUser != null) {
            throw new EshopException("你已经参与了");
        }
        final StoreBargain storeBargain = (StoreBargain)this.storeBargainService.getById((Serializable)bargainId);
        if (storeBargain == null) {
            throw new EshopException("砍价商品不存在");
        }
        final StoreBargainUser yxStoreBargainUser = StoreBargainUser.builder().bargainId(bargainId).uid(uid).bargainPrice(storeBargain.getPrice()).bargainPriceMin(storeBargain.getMinPrice()).price(BigDecimal.ZERO).status(OrderInfoEnum.BARGAIN_STATUS_1.getValue()).build();
        this.storeBargainUserMapper.insert(yxStoreBargainUser);
    }
    
    public StoreBargainUser getBargainUserInfo(final Long bargainId, final Long uid) {
        final LambdaQueryWrapper<StoreBargainUser> wrapper = (LambdaQueryWrapper<StoreBargainUser>)new LambdaQueryWrapper();
        ((wrapper.eq(StoreBargainUser::getBargainId, bargainId)).eq(StoreBargainUser::getUid, uid)).last("limit 1");
        return (StoreBargainUser)this.storeBargainUserMapper.selectOne((Wrapper)wrapper);
    }
    
    public int getBargainUserCount(final Long bargainId, final Integer status) {
        return ((this.lambdaQuery().eq(StoreBargainUser::getBargainId, bargainId)).eq(StoreBargainUser::getStatus, status)).count();
    }
    
    static {
        log = LoggerFactory.getLogger((Class)StoreBargainUserServiceImpl.class);
    }
}
